package com.zyx.pulsardemo.connector.source;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.apache.pulsar.functions.api.Record;

/**
 * @author Yaxi.Zhang
 * @since 2021/10/12 14:42
 */
public class FileRecord implements Record<String> {
    public static final String SOURCE = "Source";
    public static final String LINE = "Line-no";
    /**
     * 一行文件的实际内容
     */
    private final String content;
    /**
     * 消息属性
     */
    private final Map<String, String> props;

    public FileRecord(String content, String src, int lineNumber) {
        this.content = content;
        this.props = new HashMap<>();
        this.props.put(SOURCE, src);
        this.props.put(LINE, lineNumber + "");
    }

    @Override
    public Optional<String> getKey() {
        // 使用源文件作为key, 用于基于key的订阅等等
        return Optional.ofNullable(props.get(SOURCE));
    }

    @Override
    public Map<String, String> getProperties() {
        // 消息属性公开元数据
        return props;
    }

    @Override
    public String getValue() {
        // 消息值是原始文件内容本身
        return content;
    }
}
